<!--
  @Description:时效配置
  @Author:zqy
  @Date:2023-07-02 15:41:29
  @Last Modified by:zqy
  @Last Modified time:2023-07-02 15:41:29
-->
<template>
  <el-dialog
    ref="formDlg"
    title="时效配置"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="400px"
    @close="closeDialog"
  >
    <div style="padding-right:20px">
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="90px">
        <el-form-item label="有效天数:" prop="timeLimit" maxlength="20" placeholder="请输入编码">
          <el-input v-model="formData.timeLimit" placeholder="请输入有效天数" maxlength="4" style="width:150px">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button v-loading="submiting" size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveShareTimeout, getProductShareTimeout } from '@/api/shareConfig'
export default {
  name: 'ShareConfigTime',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detail: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        id: null,
        timeLimit: ''
      },
      formRules: {
        timeLimit: [
          { required: true, message: '请输入有效天数', trigger: 'blur' },
          { pattern: /^[0-9]*$/, message: '输入数字' }
        ]
      },
      submiting: false
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    getProductShareTimeout().then(res => {
      const { id, value } = res.data
      this.formData = {
        id, timeLimit: value
      }
    })
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        id: null,
        timeLimit: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    submitForm(type) {
      if (this.submiting) return
      this.$refs['refForm'].validate(async(valid) => {
        if (valid) {
          const { id, timeLimit } = this.formData
          const data = {
            id,
            value: timeLimit
          }

          this.submiting = true
          await saveShareTimeout(data).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: `时效配置保存成功`,
                type: 'success'
              })
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
